package com.tanhua.dubbo.server.api;

import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.LogRecord;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.*;

@Service(version = "1.0.0")
public class LogManagementApiImpl implements LogManagementApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 记录日志
     *
     * @param userId
     */
    @Override
    public void logWrite(Long userId) {
        /*Query query = Query.query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        String address = userLocation.getAddress();
*/
        LogRecord logRecord = new LogRecord();
        logRecord.setId(ObjectId.get());
        logRecord.setUserId(userId);
        logRecord.setLogTime(System.currentTimeMillis());
        logRecord.setLogAddress("安徽合肥");
        logRecord.setEquipment("Iphone X");
        mongoTemplate.insert(logRecord);
    }

    @Override
    public void logOutWrite(Long userId) {
        Query query1 = Query.query(Criteria.where("userId").is(userId)).with(Sort.by(Sort.Order.desc("logTime")));
        List<LogRecord> records = mongoTemplate.find(query1, LogRecord.class);
        LogRecord logRecord = records.get(0);//根据登录时间倒序排序
        Long logTime = logRecord.getLogTime();//登录的时间
        Long now = System.currentTimeMillis();//当前时间.退出时间
        long useTime = now - logTime;//使用时长
        Query query = Query.query(Criteria.where("id").is(logRecord.getId()));
        Update update = Update.update("useTime", useTime);
        mongoTemplate.updateMulti(query, update, LogRecord.class);
    }

    /**
     * 日志记录分页
     *
     * @param page
     * @param pagesize
     * @param sortProp
     * @param sortOrder
     * @param uid
     * @return
     */
    @Override
    public PageInfo<LogRecord> logPage(Integer page, Integer pagesize, String sortProp, String sortOrder, String uid) {
        PageInfo<LogRecord> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pagesize);

        Query query = Query.query(Criteria.where("userId").is(Convert.toLong(uid)));
        long count = mongoTemplate.count(query, LogRecord.class);
        pageInfo.setTotal(Convert.toInt(count));
        pageInfo.setPages(Convert.toInt(Math.ceil(count/pagesize)));


        Pageable pageRequest = PageRequest.of(page - 1, pagesize);
        query = Query.query(Criteria.where("userId").is(Convert.toLong(uid))).with(pageRequest);
        if (sortOrder.equals("descending")) {
            query.with(Sort.by(Sort.Order.desc("logTime")));
        }else {
            query.with(Sort.by(Sort.Order.asc("logTime")));
        }
        List<LogRecord> logRecords = mongoTemplate.find(query, LogRecord.class);

        pageInfo.setRecords(logRecords);
        return pageInfo;
    }

    /**
     * 查询用户最近的登录信息
     *
     * @return
     */
    @Override
    public LogRecord queryUserRecentActivity(Integer userId) {
        Query query = Query.query(Criteria.where("userId").is(userId)).with(Sort.by(Sort.Order.desc("logTime")));
        return mongoTemplate.findOne(query, LogRecord.class);
    }

    /**
     * 查询所有用户的最近登录时间
     *
     * @param userList
     * @return
     */
    @Override
    public List<LogRecord> queryUserListRecentActivity(List<Long> userList) {
        Query query = Query.query(Criteria.where("userId").in(userList)).with(Sort.by(Sort.Order.desc("logTime")));
        List<LogRecord> recordList = mongoTemplate.find(query, LogRecord.class);

//        Set<LogRecord> logRecordList = new TreeSet<>();
//        for (LogRecord logRecord : recordList) {
//            logRecordList.add(logRecord);
//        }

        return recordList;
    }
}
